//
//  TradeListRecordView.m
//  XTXOneToOne
//
//  Created by Canny on 2018/10/29.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "TradeListRecordView.h"
#import "TradeListRecordTableViewCell.h"

@interface TradeListRecordView ()
<
UITableViewDelegate,
UITableViewDataSource>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray*sectionMarr;
@property(nonatomic, strong)ExceptionView * exceptionView;

//shuaxinyue
@end

@implementation TradeListRecordView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self createUI];
    }
    return self;
}

-(void)setDataSource:(LTOrderDataModel *)dataSource{
    
    _dataSource =  dataSource;

    [_sectionMarr removeAllObjects];
//    dataSource.list  = [NSArray array];
    if (dataSource) {
        if (dataSource.list.count<1) {
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
            self.exceptionView.heightH = 380;
            [self.exceptionView showInView:self.tableView];

        }else{
            [self.exceptionView dismiss];
            self.exceptionView = nil;
            
        }
    }else{
        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
        self.exceptionView.heightH = 380;
        [self.exceptionView showInView:self.tableView];
        return;
    }
    
    NSMutableArray * marr = [[NSMutableArray alloc]init];
    
    for (LTOrderCellModel * data  in dataSource.list) {
        
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([TradeListRecordTableViewCell class]);
        model.className=NSStringFromClass([TradeListRecordTableViewCell class]);
        model.height = [TradeListRecordTableViewCell computeHeight:@""];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = data;
        [marr addObject:model];
    }
    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr];
    model0.headerhHeight=1;
    model0.footerHeight=1;
    [_sectionMarr addObject:model0];
    [_tableView reloadData];
    
}

-(void)createUI{
    
    _sectionMarr= [[NSMutableArray alloc]init];

    UILabel * lab1 = [[UILabel alloc]init];
    [self addSubview:lab1];
    [self setLab:lab1 withColor:RGB(32, 64, 89) andFont:18 andText:@"当前委托" andTextAlignment:NSTextAlignmentLeft];
    lab1.frame = CGRectMake(15, 0, 100, 30);
    lab1.font = [UIFont boldSystemFontOfSize:18];
    
    UIButton *  btn = [[UIButton alloc]init];
    btn.backgroundColor = MC_TEST_COLOR;
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80));
        make.top.equalTo(self.mas_top).offset(0);
        make.right.equalTo(self.mas_right).offset(-15);
        make.height.equalTo(@(30));
    }];
    [btn setTitleColor:RGB(32, 64, 89) forState:UIControlStateNormal];
    [btn setTitle:@"全部订单" forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize: 12.0];
    [btn addTarget:self action:@selector(GoToAllOrder) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView * weixin = [[UIImageView alloc]init];
    [btn addSubview:weixin];
    weixin.image = [UIImage imageNamed:@"更多btn"];
    [weixin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btn.mas_right).offset(0);
        make.centerY.equalTo(btn.mas_centerY).offset(0);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(15);
    }];
    
    
    UIButton *  btn2 = [[UIButton alloc]init];
    btn2.backgroundColor = MC_TEST_COLOR;
    [self addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(80));
        make.top.equalTo(self.mas_top).offset(0);
        make.right.equalTo(btn.mas_left).offset(-10);
        make.height.equalTo(@(30));
    }];
    [btn2 setTitleColor:RGB(32, 64, 89) forState:UIControlStateNormal];
    [btn2 setTitle:@"刷新订单" forState:UIControlStateNormal];
    btn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn2.titleLabel.font = [UIFont boldSystemFontOfSize: 12.0];
    [btn2 addTarget:self action:@selector(refreshOrder) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView * weixin2 = [[UIImageView alloc]init];
    [btn2 addSubview:weixin2];
    weixin2.image = [UIImage imageNamed:@"shuaxinyue"];
    [weixin2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(btn2.mas_right).offset(0);
        make.centerY.equalTo(btn2.mas_centerY).offset(0);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(15);
    }];
    
    
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.sectionHeaderHeight=1;
    _tableView.sectionFooterHeight=10;
    [self addSubview:_tableView];
    _tableView.frame=CGRectMake(0, 30, G_SCREENWIDTH, 370);
//    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.equalTo(self.mas_top).offset(30);
//        make.left.and.right.equalTo(self).offset(0);
//        make.bottom.equalTo(self.mas_bottom).offset(0);
//    }];
    
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}

#pragma mark tableView 代理相关


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_sectionMarr.count<1) {
        return 0;
    }
    SectionModel *sm = _sectionMarr[section];
    return sm.cells.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    SectionModel *sm = _sectionMarr[section];
//    return 1;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    SectionModel *sm = _sectionMarr[section];
//    return 1;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_sectionMarr.count<1) {
        return 0;
    }
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _sectionMarr[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    WS(weakSelf);
    if ([cm.className isEqualToString:NSStringFromClass([TradeListRecordTableViewCell class])]) {
        TradeListRecordTableViewCell *ex_cell=(TradeListRecordTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        ex_cell.block = ^(NSInteger status, LTOrderCellModel * _Nonnull dataSource) {
            if (weakSelf.actionBlock) {
                weakSelf.actionBlock(status, dataSource);
            }
        };
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)GoToAllOrder{
    if (self.allOrderBlock) {
        self.allOrderBlock();
    }
}

-(void)refreshOrder{
    if (self.refreshBlock) {
        self.refreshBlock();
    }
}

@end
